﻿using FastReport.Wizards;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace PrintLabel.Tools
{
    public  class ShippingmarkHelper
    {
        /// <summary>
        /// 获取所有模板
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> 获取所有模板()
        {
            return dbContext.dbcontext.Queryable<ReportTable>().Where(x=>x.reportStyle== "唛头模板").Select(x=>x.reportName).ToList();
        }
        public static void DownLoadShinppingmark(string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path + "\\Labels\\");
            FileInfo[] files = dir.GetFiles();
            foreach (var item in files)
            {
                File.Delete(item.FullName);
            }
            var reportlist = dbContext.dbcontext.Queryable <ReportTable>().Where(x => x.reportStyle == "唛头模板").Select(x=>x.reportName).ToList ();
            foreach (var r in reportlist)
            {
                Save(r, "唛头模板", path);
            }
        }
        public static void DownLoadCheckReport(string path)
        {
          
            var reportlist = dbContext.dbcontext.Queryable<ReportTable>().Where(x => x.reportStyle == "Report"&&x.reportName== "CheckDoc.frx").ToList();
            foreach (var r in reportlist)
            {
                Save(r.reportName, r.reportStyle, path);
            }
        }
        public static void Save(string reportname, string reportstyle, string path)
        {
            try
            {
                var report = dbContext.dbcontext.Queryable<ReportTable>().First(x => x.reportName == reportname && x.reportStyle == reportstyle);
                if (report!=null)
                {
                    var filepath = path + "\\Labels\\" + reportname;
                    FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write);
                    fs.Write(report.ReportFile, 0, report.ReportFile.Length);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                ShowMessage.ShowErrorMessagebox(new System.Windows.Forms.Form(), ex.Message);
            }
        }
    }
}
